import fs from "fs";
import path from "path";
import { Sequelize } from "sequelize";
import type { Dialect } from "sequelize";
import _sqlite3 from "sqlite3";
import { app } from "electron";

const SQLOTE_PATH = path.join(
    path.join(path.resolve(__dirname, "..", ".."), "sources"),
    "nas.sqlite"
);

/**
 * 创建一个数据库
 * @param storage 表示 url 或者 sqlite 数据库文件路径 的字符串
 * @param {"mysql" | "postgres" | "sqlite" | "mariadb" | "mssql" | "db2" | "snowflake" | "oracle"} dialect 数据库的类型
 * @returns
 */
function connectSQLite(storage: string = SQLOTE_PATH, name: string = "sqlite") {
    return new Sequelize({
        dialect: "sqlite",
        storage: storage,
        logging: console.log,
    });
}

async function chechConnect(connection: Sequelize) {
    try {
        await connection.authenticate();
        return connection;
    } catch (error) {
        console.error("Unable to connect to the database:", error);
    }
}

export { connectSQLite };
